<?php
header("content-type:text/html;charset=utf8");
header('Access-Control-Allow-Origin:*');
include './PHPExcel/PHPExcel/IOFactory.php';//引入PHPExcel类
require_once('config.php');
$uid = $_POST['uid'];
$tid = $_POST['tid'] ? : '0';
if(empty($uid)){
	$redult['status'] = '500';
	$redult['info'] = '店铺参数错误';
	exit(json_encode($redult));
}
$file = $_FILES['file'];  //上传
$hz = substr(strrchr($file["name"], '.'), 1); //文件的后缀 xls/xlxs
$newName = "5p8excel" . date('Y-m-d His', time()) . '.' . $hz;
$excelFilePath = "images/" . date('Y-m-d') . "/excel/";
if (!file_exists($excelFilePath)) {
    mkdir("$excelFilePath", 0777, true);
}
move_uploaded_file($file["tmp_name"], $excelFilePath . $newName); //保存表格文件
//exit;
$inputFileName = $excelFilePath . $newName;//读取的excel文件
date_default_timezone_set('PRC');
// 读取excel文件
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
    die('加载文件发生错误："' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$data = $sheet->toArray();//该方法读取不到图片 图片需单独处理
$imageFilePath = 'images/' . date('Y-m-d') . '/';//图片在本地存储的路径
if (!file_exists($imageFilePath)) {
    mkdir("$imageFilePath", 0777, true);
}
//处理图片
foreach ($sheet->getDrawingCollection() as $img) {
    list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($img->getCoordinates());//获取图片所在行和列
    $imageFileName = $img->getCoordinates() . mt_rand(100, 999);
    copy($img->getPath(), $imageFilePath . $imageFileName . '.' . $img->getExtension());
    //插入代码
    $startColumn = ABC2decimal($startColumn);//由于图片所在位置的列号为字母，转化为数字
    $data[$startRow - 1][$startColumn] = $imageFilePath . $imageFileName . '.' . $img->getExtension();//把图片插入到数组中

}
$data = array_slice($data, 3);
$data = array_filter(filter_array($data, $values = ['', null, false, 0, '0',[]]));
$member_sql = $mysql->query("select * from dyspw_member where id = '$uid'");
$mem_info = $member_sql->fetch_all(MYSQLI_ASSOC);
$u_name = $mem_info[0]['username'];
$u_avatar = $mem_info[0]['avatar'];
//入库处理流程，根据传入的uid确定商品属性
$time = time();
foreach ($data as $value) {
    $sql_insert = "insert into dyspw_goods (title,money,group_money,img,details,time,u_id,info,info_img,u_name,u_avatar,address_id,address_name,tid,stock,sales,is_give) values ('$value[0]','$value[1]','$value[2]','$value[3]','$value[4]','$time','$uid','$value[5]','$value[6]','$u_name','$u_avatar','0','苏州市',$tid,'$value[7]','$value[8]','$value[9]')";
    $mysql->query($sql_insert);
}

$redult['status'] = '200';
$redult['info'] = '导入成功';
exit(json_encode($redult));

/**
 * 去除多维数组中的空值
 * @author
 * @return mixed
 * @param $arr 目标数组
 * @param array $values 去除的值  默认 去除  '',null,false,0,'0',[]
 */
function filter_array($arr, $values = ['', null, false, 0, '0',[]]) {
    foreach ($arr as $k => $v) {
        if (is_array($v) && count($v)>0) {
            $arr[$k] = filter_array($v, $values);
        }
        foreach ($values as $value) {
            if ($v === $value) {
                unset($arr[$k]);
                break;
            }
        }
    }
    return $arr;
}

function ABC2decimal($abc)
{
    $ten = 0;
    $len = strlen($abc);
    for ($i = 1; $i <= $len; $i++) {
        $char = substr($abc, 0 - $i, 1);//反向获取单个字符
        $int = ord($char);
        $ten += ($int - 65) * pow(26, $i - 1);
    }
    return $ten;
}